﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKArch.WallModule
{
	public class WallThicknessEditer
	{
		public WallThicknessEditer(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public Result NewWallType(ref string message)
		{
			Reference reference = this.m_Revit.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "选择墙体");
			if (reference == null)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Wall wall = this.m_Revit.Application.ActiveUIDocument.Document.GetElement(reference) as Wall;
			if (wall == null)
			{
				message = "未选中或选择的不是一个墙实体";
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (wall.WallType.Kind != null)
			{
				message = "本功能只适用于基本墙";
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			WallType wallType = null;
			if (!this.NewWallType(wall.WallType, ref wallType, ref message))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			wall.WallType = wallType;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public bool NewWallType(WallType refWallType, ref WallType newWallType, ref string message)
		{
			List<WallThicknessInfos> wallThicknessInfos = this.GetWallThicknessInfos(refWallType);
			List<string> allWallTypeNames = this.GetAllWallTypeNames();
			EditWallThicknessForm editWallThicknessForm = new EditWallThicknessForm(refWallType.Kind, refWallType.Name, wallThicknessInfos, allWallTypeNames);
			if (editWallThicknessForm.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			bool result;
			try
			{
				newWallType = this.GetWallType(refWallType, editWallThicknessForm.NewWallTypeName, wallThicknessInfos, ref message);
				if (newWallType == null)
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				message = "新类型命名有误";
				result = false;
			}
			return result;
		}

		private List<WallThicknessInfos> GetWallThicknessInfos(WallType wallType)
		{
			List<WallThicknessInfos> list = new List<WallThicknessInfos>();
			CompoundStructure compoundStructure = wallType.GetCompoundStructure();
			if (compoundStructure == null)
			{
				double width = AssistFunc.feetToMM(wallType.Width);
                WallThicknessInfos item = new WallThicknessInfos((MaterialFunctionAssignment)1, "结构", width);
				list.Add(item);
				return list;
			}
			foreach (CompoundStructureLayer compoundStructureLayer in compoundStructure.GetLayers())
			{
				MaterialFunctionAssignment function = compoundStructureLayer.Function;
				string name = "";
                switch ((int)function)
				{
				case 1:
					name = "结构";
					break;
				case 2:
					name = "衬底";
					break;
				case 3:
					name = "保温层/空气层";
					break;
				case 4:
					name = "面层1";
					break;
				case 5:
					name = "面层2";
					break;
				default:
                    if ((int)function != 100)
					{
                        if ((int)function == 200)
						{
							name = "结构装饰";
						}
					}
					else
					{
						name = "涂膜层";
					}
					break;
				}
				double width2 = AssistFunc.feetToMM(compoundStructureLayer.Width);
				WallThicknessInfos item2 = new WallThicknessInfos(function, name, width2);
				list.Add(item2);
			}
			return list;
		}

		private List<string> GetAllWallTypeNames()
		{
			List<string> list = new List<string>();
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(WallType)).ToElements())
			{
				WallType wallType = element as WallType;
				if (wallType != null)
				{
					list.Add(wallType.Name);
				}
			}
			return list;
		}

		private WallType GetWallType(WallType currentWallType, string newWallTypeName, List<WallThicknessInfos> wallThicknessInfos, ref string message)
		{
			WallType wallType = currentWallType.Duplicate(newWallTypeName) as WallType;
			CompoundStructure compoundStructure = wallType.GetCompoundStructure();
			if (compoundStructure != null)
			{
				IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
				for (int i = 0; i < layers.Count; i++)
				{
					layers[i].Width = AssistFunc.mmToFeet(wallThicknessInfos[i].m_Width);
				}
				compoundStructure.SetLayers(layers);
				wallType.SetCompoundStructure(compoundStructure);
			}
			return wallType;
		}

		private ExternalCommandData m_Revit;
	}
}
